package com.dobby.pea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dobby.pea.common.R;
import com.dobby.pea.dto.TypeDto;
import com.dobby.pea.entity.tbOrderDetail;
import com.dobby.pea.entity.tbType;
import com.dobby.pea.entity.tbTypeSecond;
import com.dobby.pea.entity.tbUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dobby.pea.service.TypeService;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Dobby
 * @version 1.0
 */

@RestController
@RequestMapping("/api/type")
@Slf4j
public class TypeController {


    @Autowired
    private TypeService typeService;

    /**
     * 获取一级目录
     * @return
     */
    @GetMapping("/first")
    public R<List<tbType>> firstType(){

        LambdaQueryWrapper<tbType> queryWrapper = new LambdaQueryWrapper<>();

        List<tbType> list = typeService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 获取二级目录(带着一级目录）
     * @return
     */
    @GetMapping("/second")
    public R<List<TypeDto>> secondType(){

        LambdaQueryWrapper<tbType> queryWrapperlist = new LambdaQueryWrapper<>();

        List<tbType> list = typeService.list(queryWrapperlist);

        List<TypeDto> typeDtos = typeService.typeWithSecond(list);

        return R.success(typeDtos);

    }


    /**
     * 新增一级目录
     * @param type
     * @return
     */
    @PostMapping("/first")
    public R<String> saveFirst(@RequestBody tbType type){
        log.info("类别信息：{}",type);

        LambdaQueryWrapper<tbType> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(tbType::getTypename, type.getTypename());

        int count = typeService.count(queryWrapper);

        if (count<=0){
            typeService.save(type);

            return R.success("新增类别成功");
        }else{
            return R.error("已有此类别");
        }
    }


    /**
     * 新增二级目录
     * @param tbTypeSecond
     * @return
     */
    @PostMapping("/second")
    public R<String> saveSecond(@RequestBody tbTypeSecond tbTypeSecond){
        log.info("类别信息：{}",tbTypeSecond);

        return typeService.saveSecond(tbTypeSecond);

    }

    /**
     * 删除一级目录
     * @param ids
     * @return
     */
    @DeleteMapping("/first")
    public R<String> deleteFirst(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        return typeService.deleteFirst(ids);

    }


    /**
     * 删除二级目录
     * @param ids
     * @return
     */
    @DeleteMapping("/second")
    public R<String> deleteSecond(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        return typeService.deleteSecond(ids);

    }

    /**
     * 更新一级目录
     * @param type
     * @return
     */
    @PutMapping("/first")
    public R<String> updateFirst(@RequestBody tbType type){
        log.info(type.toString());
        typeService.updateById(type);
        return R.success("一级目录修改成功");
    }



    /**
     * 更新二级目录
     * @param type
     * @return
     */
    @PutMapping("/second")
    public R<String> updateSecond(@RequestBody tbTypeSecond typeSecond){
        log.info(typeSecond.toString());
        typeService.updateSecond(typeSecond);
        return R.success("二级目录修改成功");
    }




}
